package com.jiayou.handler

import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInboundHandlerAdapter
import io.netty.channel.group.ChannelGroup
import io.netty.handler.timeout.IdleState
import io.netty.handler.timeout.IdleStateEvent
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

/**
@author 李 双
@data   2020-11-19  12:39
@info
 */

class ServerIdleStateHandler(var channelGroup: ChannelGroup) : ChannelInboundHandlerAdapter() {
    override fun userEventTriggered(ctx: ChannelHandlerContext, evt: Any?) {
        if (evt is IdleStateEvent) {
            when (evt.state()) {
                IdleState.READER_IDLE -> {
                    channelGroup.remove(ctx.channel())
                    println("当前channel的个数： ${channelGroup.size}")
                    ctx.close()
                }
                IdleState.WRITER_IDLE -> {
                    println(ctx.channel())
                    println("当前channel的数量：" + channelGroup.size)
                }
                IdleState.ALL_IDLE -> println("${ctx.channel()?.remoteAddress()}读写空闲")
                else -> println("${ctx.channel()?.remoteAddress()}事件可能为NULL")
            }
        }
    }
}